<html>
<head><meta charset="utf-8"><title>Why is HashMap in std and not alloc? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html">Why is HashMap in std and not alloc?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211396220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396220">(Sep 27 2020 at 03:40)</a>:</h4>
<p>It would make my life a lot easier right now if HashMap were in alloc: <a href="https://github.com/rust-lang/rust/pull/77253/commits/45250f4bb468bb1a48f616b0b6dfca733836c2c0">https://github.com/rust-lang/rust/pull/77253/commits/45250f4bb468bb1a48f616b0b6dfca733836c2c0</a> is broken because it can't refer to upstream crates. Is there any reason not to move it, assuming that <code>std</code> re-exports it?</p>



<a name="211396247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396247">(Sep 27 2020 at 03:41)</a>:</h4>
<p>HashMap needs not only <code>alloc</code> but also <code>some_randomization_source_from_somewhere</code></p>



<a name="211396295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396295">(Sep 27 2020 at 03:42)</a>:</h4>
<p>that's a trait though, I thought?</p>



<a name="211396299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396299">(Sep 27 2020 at 03:42)</a>:</h4>
<p>could the trait be in alloc and the default implementation be in std? or does that break coherence?</p>



<a name="211396428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396428">(Sep 27 2020 at 03:46)</a>:</h4>
<p>Well, <code>hashbrown</code>, the crate that provides the <code>HashMap</code> that <code>std</code> uses, can be used in a <code>no_std</code> setting if <code>alloc</code> is available, and I'm not sure how it gets its randomization in that case.</p>



<a name="211396508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396508">(Sep 27 2020 at 03:49)</a>:</h4>
<p>looks like it uses <code>ahash</code></p>



<a name="211396520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396520">(Sep 27 2020 at 03:49)</a>:</h4>
<p>let me see what std uses</p>



<a name="211396565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396565">(Sep 27 2020 at 03:50)</a>:</h4>
<p>looks like it's OS-specific: <a href="https://doc.rust-lang.org/src/std/collections/hash/map.rs.html#2525">https://doc.rust-lang.org/src/std/collections/hash/map.rs.html#2525</a></p>



<a name="211396571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396571">(Sep 27 2020 at 03:50)</a>:</h4>
<p>is there interest in making hashmaps available in alloc? It seems like it would be useful for things besides intra-doc links ;)</p>



<a name="211396580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396580">(Sep 27 2020 at 03:51)</a>:</h4>
<blockquote>
<p>aHash uses the hardware AES instruction on x86 processors to provide a keyed hash function. aHash is not a cryptographically secure hash.</p>
</blockquote>



<a name="211396702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396702">(Sep 27 2020 at 03:54)</a>:</h4>
<p>is there interest? Yes, i'm sure.</p>
<p>can it be done so simply? Well it hasn't been done yet so...</p>



<a name="211396865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211396865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211396865">(Sep 27 2020 at 03:58)</a>:</h4>
<p>let me fix <a href="https://github.com/rust-lang/rust/issues/77254">https://github.com/rust-lang/rust/issues/77254</a> tonight and then I'll get back to you</p>



<a name="211397710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211397710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211397710">(Sep 27 2020 at 04:23)</a>:</h4>
<p>We'd have to pull <code>hashbrown</code> into <code>alloc</code> to get <code>HashMap</code> in there, to avoid a circular dependency</p>



<a name="211397748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211397748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211397748">(Sep 27 2020 at 04:24)</a>:</h4>
<p>(apart from the question of default hasher)</p>



<a name="211417785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211417785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211417785">(Sep 27 2020 at 13:56)</a>:</h4>
<p>oh right</p>



<a name="211417788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211417788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211417788">(Sep 27 2020 at 13:56)</a>:</h4>
<p>yeah that's unfortunate</p>



<a name="211433344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211433344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211433344">(Sep 27 2020 at 20:33)</a>:</h4>
<p>Could maybe this be done with the re-export in std somehow?  IIRC the part that needs the fancy stuff is the <code>S = RandomState</code> -- could it be in <code>alloc</code> without that default, and have the <code>std</code> version be <code>type HashMap&lt;K, V, S = RandomState&gt; = alloc::HashMap&lt;K, V, S&gt;;</code>?</p>
<p>(Assuming defaults even work right in type aliases...)</p>



<a name="211435828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211435828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211435828">(Sep 27 2020 at 21:41)</a>:</h4>
<p>defaults do work right in type aliases — I use <code>type Result&lt;T, E = MyError&gt; = core::result::Result&lt;T, E&gt;;</code> all the time.</p>



<a name="211436534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211436534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211436534">(Sep 27 2020 at 21:59)</a>:</h4>
<p>Are you sure? I was under the impression that you had to use <code>type Result&lt;T&gt; = core::result::Result&lt;T, MyError&gt;</code> instead.</p>



<a name="211436537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211436537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211436537">(Sep 27 2020 at 21:59)</a>:</h4>
<p>Maybe they fixed defaults in type aliases since I last checked...</p>



<a name="211437919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211437919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211437919">(Sep 27 2020 at 22:37)</a>:</h4>
<p>There's also <code>HashMap::new</code> and <code>with_capacity</code> that are only defined for <code>S = RandomState</code>. I don't know how we'd do that through a type alias, unless we employ language hacks to bypass coherence.</p>



<a name="211439119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439119">(Sep 27 2020 at 23:08)</a>:</h4>
<p>is there a reason they're only defined for <code>S = RandomState</code>? <code>new</code> just delegates to <code>Default</code>, which doesn't have the bound: <a href="https://doc.rust-lang.org/nightly/std/collections/struct.HashMap.html#impl-Default">https://doc.rust-lang.org/nightly/std/collections/struct.HashMap.html#impl-Default</a></p>



<a name="211439126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439126">(Sep 27 2020 at 23:08)</a>:</h4>
<p>this annoys me a lot when working with FxHashMap, I have to call <code>default()</code> instead of <code>new</code> even though they do the same thing</p>



<a name="211439137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439137">(Sep 27 2020 at 23:09)</a>:</h4>
<p>I think instead that could just require <code>S: Default</code></p>



<a name="211439206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439206">(Sep 27 2020 at 23:10)</a>:</h4>
<p>making a pr for that now</p>



<a name="211439218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439218">(Sep 27 2020 at 23:11)</a>:</h4>
<p>oh hmm</p>
<div class="codehilite"><pre><span></span><code>error[E0283]: type annotations needed for `std::collections::HashMap&lt;i32, i32, S&gt;`
  --&gt; library/std/benches/hash/map.rs:17:21
   |
17 |         let mut m = HashMap::new();
   |             -----   ^^^^^^^^^^^^ cannot infer type for type parameter `S`
   |             |
   |             consider giving `m` the explicit type `std::collections::HashMap&lt;i32, i32, S&gt;`, where the type parameter `S` is specified
   |
   = note: cannot satisfy `_: std::default::Default`
   = note: required by `std::collections::HashMap::&lt;K, V, S&gt;::new`
</code></pre></div>



<a name="211439234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439234">(Sep 27 2020 at 23:11)</a>:</h4>
<p>and I can't add the default either:</p>
<div class="codehilite"><pre><span></span><code>error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions.
   --&gt; library/std/src/collections/hash/map.rs:205:12
    |
205 | impl&lt;K, V, S: Default = RandomState&gt; HashMap&lt;K, V, S&gt; {
    |            ^
    |
    = note: `#[deny(invalid_type_param_default)]` on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #36887 &lt;https://github.com/rust-lang/rust/issues/36887&gt;
</code></pre></div>



<a name="211439430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439430">(Sep 27 2020 at 23:16)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F/near/211437919">said</a>:</p>
<blockquote>
<p>There's also <code>HashMap::new</code> and <code>with_capacity</code> that are only defined for <code>S = RandomState</code>. I don't know how we'd do that through a type alias, unless we employ language hacks to bypass coherence.</p>
</blockquote>
<p>anyway this doesn't seem like a big deal, you don't <em>have</em> to go through the type alias:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// std</span>
<span class="k">type</span> <span class="nc">HashMap</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RandomState</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">alloc</span>::<span class="n">HashMap</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="n">S</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="o">&gt;</span><span class="w"> </span><span class="n">alloc</span>::<span class="n">HashMap</span><span class="o">&lt;</span><span class="n">K</span><span class="p">,</span><span class="w"> </span><span class="n">V</span><span class="p">,</span><span class="w"> </span><span class="n">RandomState</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="211439712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439712">(Sep 27 2020 at 23:24)</a>:</h4>
<p>That shouldn't build due to coherence violation</p>



<a name="211439723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211439723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211439723">(Sep 27 2020 at 23:25)</a>:</h4>
<p>We have some hacks that allow that for primitives though</p>



<a name="211448905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/211448905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ahmed Charles <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#211448905">(Sep 28 2020 at 03:37)</a>:</h4>
<p>It seems that type inference doesn't account for defaults, which is why <code>HashMap&lt;i32, i32&gt;::new()</code> continues to compile but <code>HashMap::new()</code> does not.</p>



<a name="212055244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212055244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212055244">(Oct 02 2020 at 10:04)</a>:</h4>
<p>the solution to this is to unify core/alloc/std and make them feature flags</p>



<a name="212094775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212094775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212094775">(Oct 02 2020 at 16:04)</a>:</h4>
<p>wouldn't that interfere with <em>all</em> the external crates that sit between core and std? <br>
at least: <code>cfg-if</code>, <code>compiler_builtins</code>, <code>hashbrown</code>, <code>libc</code>, <code>rustc-demangle</code>, <code>unwind</code></p>



<a name="212094889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212094889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212094889">(Oct 02 2020 at 16:05)</a>:</h4>
<p>cfg_if is just a macro, it's technically valid no_core even. compiler_builtins is part of core i think, the rest are "above core inside of std"</p>



<a name="212110337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212110337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212110337">(Oct 02 2020 at 18:22)</a>:</h4>
<p>They may have to be pulled in directly as modules rather than crates</p>



<a name="212110392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212110392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212110392">(Oct 02 2020 at 18:23)</a>:</h4>
<p>or rely on a minimal libcore that just defines lang items</p>



<a name="212111379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212111379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212111379">(Oct 02 2020 at 18:32)</a>:</h4>
<p><code>compiler_builtins</code> can be compiled separately, the methods it provides are only needed at link time.</p>



<a name="212111436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212111436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212111436">(Oct 02 2020 at 18:33)</a>:</h4>
<p>I think that unifying std/alloc/core would require a full RFC since there are many issues that need to be addressed.</p>



<a name="212111498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212111498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212111498">(Oct 02 2020 at 18:33)</a>:</h4>
<p>For example, how can I have a Linux no_std binary that doesn't depend on libc since std depends on libc.</p>



<a name="212124501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212124501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212124501">(Oct 02 2020 at 20:25)</a>:</h4>
<p>There's definitely a bunch of issues to resolve, but <code>libc</code> shouldn't be a problem. It is already <code>no_core</code>, and on platforms that don't have a C library it automatically becomes an empty crate.</p>



<a name="212129315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212129315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212129315">(Oct 02 2020 at 21:08)</a>:</h4>
<p><span class="user-mention silent" data-user-id="256759">boats</span> <a href="#narrow/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F/near/212055244">said</a>:</p>
<blockquote>
<p>the solution to this is to unify core/alloc/std and make them feature flags</p>
</blockquote>
<p>fwiw we're talking about doing this in the error handling project group as part of our effort to move the Error trait into core</p>



<a name="212129440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212129440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212129440">(Oct 02 2020 at 21:10)</a>:</h4>
<p>here's the relevant thread <a href="#narrow/stream/257204-project-error-handling/topic/std.2Falloc.2Fcore">https://rust-lang.zulipchat.com/#narrow/stream/257204-project-error-handling/topic/std.2Falloc.2Fcore</a></p>



<a name="212130051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212130051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212130051">(Oct 02 2020 at 21:17)</a>:</h4>
<p>tho I wanna highlight this comment from Manishearth which points out that this could also be solved by allowing multiple crates in one package where coherence rules only apply to the whole group <a href="#narrow/stream/257204-project-error-handling/topic/std.2Falloc.2Fcore/near/211830027">https://rust-lang.zulipchat.com/#narrow/stream/257204-project-error-handling/topic/std.2Falloc.2Fcore/near/211830027</a></p>



<a name="212130170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212130170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212130170">(Oct 02 2020 at 21:19)</a>:</h4>
<p>also i think the allocation randomness source has some overlap with issues that I've come up against with my Backtrace in core proof of concept, which we talked about here <a href="#narrow/stream/213817-t-lang/topic/global.20hooks.20and.20their.20relation.20to.20traits.20and.20vtables">https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/global.20hooks.20and.20their.20relation.20to.20traits.20and.20vtables</a> and here <a href="#narrow/stream/257204-project-error-handling/topic/Global.20Hooks">https://rust-lang.zulipchat.com/#narrow/stream/257204-project-error-handling/topic/Global.20Hooks</a></p>



<a name="212134742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212134742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212134742">(Oct 02 2020 at 22:13)</a>:</h4>
<p>FWIW this would make me happy from a purely selfish view of using more intra-doc links in std <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span>  <a href="https://github.com/rust-lang/rust/issues/74481">https://github.com/rust-lang/rust/issues/74481</a></p>



<a name="212140990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212140990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212140990">(Oct 02 2020 at 23:37)</a>:</h4>
<p>Would a fair summary so far be that we're all for it, we just need to RFC the fine details?</p>
<p>Does anyone care to take a stance <strong>opposing</strong> the merging of core/alloc/std ?</p>



<a name="212141059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212141059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212141059">(Oct 02 2020 at 23:38)</a>:</h4>
<p>I disagree personally that an rfc is the right step, I think we should be able to merge them without user-visible effects (like linkage changes) other than more flexible impls internally</p>



<a name="212141176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212141176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212141176">(Oct 02 2020 at 23:41)</a>:</h4>
<p>Well, an RFC "might not hurt" because it is a fairly big change to the guts of rust and people will want to have ample warning about it. Even if the change over <em>could</em> be done without any direct disruption to most users.</p>



<a name="212141369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212141369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212141369">(Oct 02 2020 at 23:45)</a>:</h4>
<p>A T-compiler MCP laying out the design, maybe, but I don't see why the community would need to weigh in on what is ultimately an internal decision? Like, I guess I do not see it as that big a change</p>



<a name="212142138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212142138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212142138">(Oct 02 2020 at 23:59)</a>:</h4>
<p>It's not about them weighing in specifically. I don't think that most people would have anything to say about the process.</p>
<p>It's about the community perception: big improvements get RFCs, and they get a little publicity because of it. This would be a big improvement that would help in areas X, Y, Z etc, so it gets an RFC and gets the associated publicity. Most intermediate and advanced Rust programmers have an <em>understanding</em> of how the standard library works: There's the <code>core</code> crate, and then there's <code>alloc</code>, and then there's <code>libc</code> and other OS stuff, and you put it all together and get <code>std</code>, and a ton of what's "in" <code>std</code> is re-exports from <code>core</code> and <code>alloc</code>, but some of it is "new" because it's OS related (like checking the clock).</p>
<p>So if you go through a big process and make it so that the common notion of how the standard library is put together is suddenly <em>wrong</em>, then you need to <strong>communicate</strong> that with people. Even if technically their code continues to compile.</p>



<a name="212142760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212142760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jane Lusby <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212142760">(Oct 03 2020 at 00:08)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> I know <span class="user-mention" data-user-id="132040">@Manish Goregaokar</span> is somewhat opposed, as they noted in the comment I linked</p>



<a name="212145292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212145292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212145292">(Oct 03 2020 at 00:58)</a>:</h4>
<p>I guess I feel RFC is the wrong process; I would be all in favor of blog post(s) on inside rust. I think an MCP for the compiler team would contain about everything the RFC would for something of this magnitude, but avoid the checkboxes and other painful bits of rfcs.</p>



<a name="212146583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212146583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212146583">(Oct 03 2020 at 01:34)</a>:</h4>
<p>a blog post would also suffice</p>



<a name="212146660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212146660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212146660">(Oct 03 2020 at 01:36)</a>:</h4>
<p>At the very least there should be some sort of design doc where people can point out potential problems with the proposed approach.</p>



<a name="212147326"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20is%20HashMap%20in%20std%20and%20not%20alloc%3F/near/212147326" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20is.20HashMap.20in.20std.20and.20not.20alloc.3F.html#212147326">(Oct 03 2020 at 01:55)</a>:</h4>
<p>Yep, I think that's an MCP for compiler team. If we want to make changes to the end user interface, then I think an RFC is warranted, but ultimately that's a t-libs call.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>